//
//  MoreViewController.m
//  Cartoon
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "MoreViewController.h"
#import "MoreTableViewCell.h"
#import "Recommend.h"
#import "CartoonDetailsViewController.h"
@interface MoreViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *arr;
@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight - 49 - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = false;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    
    [_tableView registerClass:[MoreTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self handleData];
    [self addHeader];
    [self loadMoreData];
    self.arr = [NSMutableArray array];
    // Do any additional setup after loading the view.
}

- (void)handleData
{
    

    NSString *str =[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/categoryList/uin/0/local_version/3.1.0/channel/1001/guest_id/491A80FA-091B-4330-B939-7DE39BB24F7F/category_id/%@/page/1/listcnt/20",self.textID];
    
    [NetHandler dataWithUrl:str complant:^(id data) {
        
        NSMutableArray *array = [data objectForKey:@"data"];
        
        for (NSMutableDictionary * dic in array) {
            
            Recommend *recommend = [[Recommend alloc] init];
            
            [recommend setValuesForKeysWithDictionary:dic];
            
            [self.arr addObject:recommend];
         
        }
        [_tableView reloadData];
    }];
    

}
- (void)addHeader
{
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [self handleData];
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
        });
    }];
    
    //设置刷新控件
    self.tableView.header = header;
}
//上拉加载
- (void)loadMoreData
{
    __block  NSInteger i = 2;
    NSLog(@"%ld",i);
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        
        NSString *newstr = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/categoryList/uin/0/local_version/3.1.0/channel/1001/guest_id/491A80FA-091B-4330-B939-7DE39BB24F7F/category_id/%@/page/%ld/listcnt/20",self.textID,i];
        i++;
        [NetHandler dataWithUrl:newstr complant:^(id data) {
            
            NSMutableArray *array = [data objectForKey:@"data"];
            
            for (NSMutableDictionary * dic in array) {
                
                Recommend *recommend = [[Recommend alloc] init];
                
                [recommend setValuesForKeysWithDictionary:dic];
              
                [self.arr addObject:recommend];
                [self.tableView.footer endRefreshing];
            }
//            self.arr = (NSMutableArray *)[self.arr arrayByAddingObjectsFromArray:array];

            [_tableView reloadData];
        }];
        
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{
    
    return 1;
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.arr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    Recommend *recommend = [self.arr objectAtIndex:indexPath.row];
    
    cell.titleLabel.text = recommend.title;
    cell.namelabel.text = recommend.artist_name;
    cell.lastupLabel.text = recommend.lastup;
    cell.gradeLabel.text = recommend.grade_ave;
    cell.starView.starNum = recommend.grade;
    [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:recommend.cover_url]placeholderImage:[UIImage imageNamed:@"5"]];
    
    
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CartoonDetailsViewController *carDetail = [[CartoonDetailsViewController alloc] init];
    
    
    Recommend *recommend = [self.arr objectAtIndex:indexPath.row];
    
    carDetail.idtext = recommend.comic_id;
    
    [self.navigationController pushViewController:carDetail animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
